﻿using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace RegexTester.UI
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 执行命令单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 执行ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            textBox3.Text = "";
            if (!String.IsNullOrEmpty(textBox1.Text) && !String.IsNullOrEmpty(textBox2.Text))
            {
                Regex reg = new Regex(textBox1.Text);
                MatchCollection matches = reg.Matches(textBox2.Text);
                if (matches != null && matches.Count != 0)
                {
                    for (Int32 i = 0; i < matches.Count; i++)
                        textBox3.Text += String.Format("[{0}]:{1}\r\n", i, matches[i].Value);
                }
                else textBox3.Text = "提供的字符串无法匹配正则表达式!";

                //记录历史记录
                DAL.TestHistory.Instance.Add(new Model.TestHistory()
                {
                    Regex = textBox1.Text,
                    Source = textBox2.Text,
                    TestDatetime = DateTime.Now
                });
            }
        }

        /// <summary>
        /// 查看历史记录的按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            ViewHistoryEx viewHistory = new ViewHistoryEx();
            if (viewHistory.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                //Model.TestHistory historyObj = viewHistory.SelectHistoryRecord;
                //if (historyObj != null)
                //{
                //    textBox1.Text = historyObj.Regex;
                //    textBox2.Text = historyObj.Source;
                //}
            }
        }
    }
}
